<?php
function delAcct($id)
{	
	$data = SQLDelete ('core_accounts', 'accountID = "'.$id.'"');
	$data = SQLDelete ('core_accounts_contacts', 'accountID = "'.$id.'"');
}

function getFLName($uID)
{
	$tables = array();
	$tables[0] = 'site_users';
	
	$fields = array();
	$fields[0] = 'firstName';
	$fields[1] = 'lastName';
		
	$whereClause = 'userName="'.$uID.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	return $data['firstName'][0].'&nbsp;'.$data['lastName'][0];
}
function getInitItem($default, $type)
{
	
	$tables = array();
	$tables[0] = 'core_accounts';
	
	$fields = array();
	$fields[0] = 'accountID';
	
	$whereClause = ($default == 'null' ? 'accountType = "'.$type.'" OR accountType = "mixed"' : '(accountType = "'.$type.'" OR accountType = "mixed") AND accountID = "'.$default.'"');
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		return $data['accountID'][0];
	}
	else
	{
		return 'noEntries';
	}
}

function getAccountStats($id)
{
	$tables = array();
	$tables[0] = 'core_accounts';
	
	$fields = array();
	$fields[0] = 'dateMade';
	$fields[1] = 'dateUpdated';
	$fields[2] = 'updatedBy';
	
	$whereClause = 'accountID = "'.$id.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	$ret = array();
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		$ret['updatedBy'] = getFLName($data['updatedBy'][0]);
		$ret['created'] = dateParser(explode('-', $data['dateMade'][0]));
		$ret['updated'] = dateParser(explode('-', $data['dateUpdated'][0]));
	}
	else
	{
		$ret['updatedBy'] = 'N/A';
		$ret['created'] = 'N/A';
		$ret['updated'] = 'N/A';
	}
		
	return $ret;
}

function listAccounts($type)
{
	$tables = array();
	$tables[0] = 'core_accounts a';
	$tables[1] = 'core_accounts_contacts c';
	
	$fields = array();
	$fields[0] = 'a.accountID id';
	$fields[1] = 'c.detail name';
	
	$whereClause = 'a.accountID = c.accountID AND ((a.accountType = "'.$type.'" OR a.accountType = "mixed") AND c.type = "_name") ORDER BY c.detail ASC';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	$output = '';
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		for ($i=0; $i<sizeOf($data['id']); $i++)
		{
			$output .= '<option value="'.INDEX.'accounts'.EXT.'type='.$type.'&entry='.$data['id'][$i].'">'.$data['name'][$i].'</option>';
		}
	}
	else
	{
		$output = '<option value="'.INDEX.'accounts'.EXT.'type='.$type.'">No Entries Avail.</option>';
	}
	return $output;
}

function getThisAccount($id, $type)
{
	$tables = array();
	$tables[0] = 'core_accounts_contacts';
	
	$fields = array();
	$fields[0] = 'detail';
	
	
	
	if($type == "_address")
	{
		$whereClause = 'accountID = "'.$id.'" AND type = "'.$type.'" ORDER BY priority DESC';
		$data = SQLRecover($tables, $fields, $whereClause);
		
		if($data['queryStatus'] == 'DB Query success!')
		{
			$return = $data['detail'][0];
			
			if(isset($data['detail'][1]))
			{
				$return .= '<div class="altAddress">
				<div class="aalink">[<a href="'.INDEX.'404" class="otherBranch" title="Click to display/hide other branches">other branches...</a>]</div>
				<div class="altAddList">
				';
				for ($i=1; $i<sizeOf($data['detail']); $i++)
				{
					$j = sizeOf($data['detail'])-1;
					$return .= $data['detail'][$i].($i == $j ? '' : '<br />');
				}
				$return .= '</div></div>';
			}
			return $return;
		}
		else
		{
			return 'Address not specified.';
		}
	}
	else
	{
		$whereClause = 'accountID = "'.$id.'" AND type = "'.$type.'"';
		$data = SQLRecover($tables, $fields, $whereClause);
		
		return ($data['queryStatus'] == 'DB Query success!' ? $data['detail'][0] : 'No Entry Found.');
	}
}

function getAccountDetails($id)
{
	$tables = array();
	$tables[0] = 'core_accounts_contacts';
	
	$fields = array();
	$fields[0] = 'detail';
	$fields[1] = 'type';
	
	$whereClause = 'accountID = "'.$id.'" AND priority > 0 ORDER BY priority ASC';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	$toReturn = array();
	$toReturn[0][1] = '&nbsp;';
	$toReturn[0][0] = '&nbsp;';
	$toReturn[1][1] = '&nbsp;';
	$toReturn[1][0] = '&nbsp;';

	if($data['queryStatus'] == 'DB Query success!')
	{
		
		for ($i=0; $i<sizeOf($data['detail']); $i++)
		{
			$modi = $i % 2; 
			if($modi == 0)
			{
				if($i == 0)
				{
					$toReturn[0][1] = '';
					$toReturn[0][0] = '';
				}
				$toReturn[0][1] .= $data['detail'][$i].'<br/>';
				$toReturn[0][0] .= $data['type'][$i].'<br/>';
			}
			else
			{
				if($i == 1)
				{
					$toReturn[1][1] = '';
					$toReturn[1][0] = '';
				}
				$toReturn[1][1] .= $data['detail'][$i].'<br/>';
				$toReturn[1][0] .= $data['type'][$i].'<br/>';
			}
		}
	}
	else
	{
		$toReturn[0][1] .= '&nbsp;';
		$toReturn[0][0] .= '&nbsp;';
		$toReturn[1][1] .= '&nbsp;';
		$toReturn[1][0] .= '&nbsp;';
	}
	return $toReturn;
	
}
?>